import { contextBridge, ipcRenderer } from 'electron';
import { IItem } from '../type';

declare global {
    interface Window {
        exposeAPI: {
            searchContentChange: (context: string) => Promise<IItem[]>;
            setWindowHeight: (height: number) => undefined;
            notification: (title: string, body: string) => undefined;
            hideWindow: () => undefined;
            showMessageBox: (opt: Electron.ShowMessageBoxOptions) => undefined;
            getSelectionIndex: () => Promise<number>;
            onMainWindowShow: (callback: () => undefined) => undefined;
            onSelectionChange: (callback: (index: number) => undefined) => undefined;
            onResetFace: (callback: () => undefined) => undefined;
        }
    }
}

contextBridge.exposeInMainWorld('exposeAPI', {
    searchContentChange: (context: string) => ipcRenderer.invoke('searchContextChange', context),
    setWindowHeight: (height: number) => ipcRenderer.invoke('setWindowHeight', height),
    notification: (title: string, body: string) => ipcRenderer.invoke('notification', title, body),
    hideWindow: () => ipcRenderer.invoke('hideWindow'),
    showMessageBox: (opt: Electron.ShowMessageBoxOptions) => ipcRenderer.invoke('showMessageBox', opt),
    getSelectionIndex: () => ipcRenderer.invoke('getSelectionIndex'),
    onMainWindowShow: (callback: () => undefined) => ipcRenderer.on('mainWindowShow', event => callback()),
    onSelectionChange: (callback: (index: number) => undefined) => ipcRenderer.on('selectionChange', (event, index: number) => { callback(index); }),
    onResetFace: (callback: () => undefined) => ipcRenderer.on('resetFace', event => callback()),
});
